package org.chupito.search;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.bean.ViewScoped;

import org.jboss.logging.Logger;

@ViewScoped
public class InvoiceSearch implements SearchObject {
	
	private String code = "";
	
	private Date billingDateFrom = null;
	private Date billingDateTo = null;
	
	public Boolean payTax = Boolean.FALSE;
	
	public static final String OPERATOR_AND = "AND";
	public static final String OPERATOR_OR = "OR";
	
	private String operator = OPERATOR_AND;
	
	private SimpleDateFormat formater = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
	
	public Date getBillingDateFrom() {
		return billingDateFrom;
	}
	
	public Date getBillingDateTo() {
		return billingDateTo;
	}
	
	public String getCode() {
		return code;
	}
	
	public Boolean getPayTax() {
		return payTax;
	}
	
	public void setBillingDateTo(Date billingDateTo) {
		this.billingDateTo = billingDateTo;
	}
	
	public void setBillingDateFrom(Date billingDate) {
		this.billingDateFrom = billingDate;
	}
	
	public void setCode(String code) {
		this.code = code;
	}
	
	public void setPayTax(Boolean payTax) {
		this.payTax = payTax;
	}
	
	public InvoiceSearch() {
		// TODO Auto-generated constructor stub
	}
	
	public String getOperator() {
		return operator;
	}
	
	public void setOperator(String operator) {
		this.operator = operator;
	}
	
	@Override
	public String toWhere() {
		
		String sWhere = " where ";
		
		if (!code.isEmpty()) {
			sWhere += " t.code = '" + code + "' " + operator;  
		}
		
		if (billingDateFrom != null) {
			
			sWhere += " t.billingDate >= '" + formater.format(billingDateFrom) + "' " + operator;
		}
		
		if (billingDateTo != null) {
			sWhere += " t.billingDate <= '" + formater.format(billingDateTo.getTime()) + "' " + operator;
		}
		
		sWhere += " t.payTax = " + payTax + " "; 
		
		Logger.getLogger(InvoiceSearch.class).info(sWhere);
		
		return (sWhere.length() < 7)?"":sWhere;
	}
}
